import 'package:flutter/material.dart';

import 'package:get/get.dart';

import '../controllers/login_controller.dart';

class LoginView extends GetView<LoginController> {
  const LoginView({super.key});

  @override
  Widget build(BuildContext context) {
    final controller = Get.put(LoginController());

    return Scaffold(
      appBar: AppBar(title: const Text('Login'), centerTitle: true),
      body: SingleChildScrollView(
        child: Container(
          margin: EdgeInsets.fromLTRB(16.0, 8.0, 16.0, 8.0),
          width: double.infinity,
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.stretch,
            children: [
              _buildForm(),
              SizedBox(height: 8.0),
              _buildSingleChildScrollView(),
            ],
          ),
        ),
      ),
    );
  }

  Widget _buildForm() {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.stretch,
      children: [
        TextField(
          keyboardType: TextInputType.phone,
          controller: controller.phoneNumberController,
          maxLength: 11,
          decoration: InputDecoration(
            hintText: '请输入手机号',
            // counter: Text(''),
            counterText: '',
            border: OutlineInputBorder(),
            contentPadding: EdgeInsets.fromLTRB(12, 20, 12, 0),
            // contentPadding: EdgeInsets.only(bottom: 0.0),
            suffixIcon: IconButton(
              onPressed: () {
                controller.cleanPhoneNumberInputText();
              },
              iconSize: 16.0,
              icon: Icon(Icons.close),
            ),
          ),
          onChanged: (text) {
            controller.updatePhoneNumberInputText(text);
          },
        ),
        SizedBox(height: 8.0),
        Flex(
          direction: Axis.horizontal,
          children: [
            Expanded(
              flex: 3,
              child: TextField(
                keyboardType: TextInputType.number,
                controller: controller.smsCodeController,
                maxLength: 6,
                decoration: InputDecoration(
                  hintText: '请输入验证码',
                  // counter: Text(''),
                  counterText: '',
                  border: OutlineInputBorder(),
                  contentPadding: EdgeInsets.fromLTRB(12, 20, 12, 0),
                  // contentPadding: EdgeInsets.only(bottom: 0.0),
                  suffixIcon: IconButton(
                    onPressed: () {
                      controller.cleanSMSCodeInputText();
                    },
                    iconSize: 16.0,
                    icon: Icon(Icons.close),
                  ),
                ),
                onChanged: (text) {
                  controller.updateSMSCodeInputText(text);
                },
              ),
            ),
            SizedBox(width: 8.0),
            Expanded(
              flex: 2,
              child: Obx(
                () => ElevatedButton(
                  onPressed:
                      controller.countDownController.isComplete
                          ? () {
                            controller.getSMSCode();
                          }
                          : null,
                  child: Text(
                    controller.countDownController.isComplete
                        ? '获取验证码'
                        : '${controller.countDownController.remainingSeconds}S',
                  ),
                ),
              ),
            ),
          ],
        ),
        SizedBox(height: 8.0),
        ElevatedButton(
          onPressed: () {
            controller.login();
          },
          child: Text('登录'),
        ),
      ],
    );
  }

  Widget _buildSingleChildScrollView() {
    return Column(
      children: [_buildSMSCode(), SizedBox(height: 8.0), _buildLoginInfo()],
    );
  }

  Widget _buildSMSCode() {
    return Obx(
      () => SelectableText(controller.smsCode.value, style: TextStyle()),
    );
  }

  Widget _buildLoginInfo() {
    return Obx(
      () => Text(controller.info.isEmpty ? '' : controller.info.toString()),
    );
  }
}
